package com.softeem.hello.service;

import com.softeem.hello.dao.UserMapper;
import com.softeem.hello.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean registry(User user) {
        //
        User u = userMapper.selectByUsername(user.getUsername());
        if(Objects.nonNull(u)){
            return false;
        }
        return userMapper.insert(user);
    }

    @Override
    public boolean login(String username,String password) {
        User u = userMapper.selectByUsername(username);
        if(Objects.nonNull(u) && Objects.equals(password,u.getPassword())){
            return true;
        }
        return false;
    }

    @Override
    public boolean remove(Integer id) {
        return userMapper.delete(id);
    }

    @Override
    public boolean modify(Integer id, String pwd) {
        User user = new User();
        user.setId(id);
        user.setPassword(pwd);
        return userMapper.update(user);
    }

    @Override
    public List<User> list() {
        return userMapper.selectAll();
    }
}
